package sharetime.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

@SuppressWarnings("unchecked")
public class ClassHelper {

	public static Object instantiateClass(Class clazz, Object constructorArgs[])
			throws ClassNotFoundException, SecurityException,
			NoSuchMethodException, IllegalArgumentException,
			InstantiationException, IllegalAccessException,
			InvocationTargetException {
		Class args[] = new Class[constructorArgs.length];
		for (int i = 0; i < constructorArgs.length; i++)
			if (constructorArgs == null)
				args[i] = null;
			else
				args[i] = constructorArgs[i].getClass();

		Constructor ctor = null;
		if (clazz.getConstructors().length == 1)
			ctor = clazz.getConstructors()[0];
		else
			ctor = clazz.getConstructor(args);
		return ctor.newInstance(constructorArgs);
	}

	public static Object instantiateClass(String name, Object constructorArgs[])
			throws ClassNotFoundException, SecurityException,
			NoSuchMethodException, IllegalArgumentException,
			InstantiationException, IllegalAccessException,
			InvocationTargetException {
		Class clazz = ClassLoaderUtils.loadClass(name,
				sharetime.util.ClassHelper.class);
		return instantiateClass(clazz, constructorArgs);
	}
}
